//Copyright (c) 2010, Goodwine & Tibia ML
//Licensed under new BSD License.

// Load icons.
var tmlIcon = exhIcon = null;
var opLinkPages;
chrome.extension.sendRequest({'icons' : ['tml', 'exh']}, function (value) {
	tmlIcon = value[0];
	exhIcon = value[1];
});
chrome.extension.sendRequest({'action' : 'opLinkPages'}, function (value) { opLinkPages = value; });
chrome.extension.sendRequest({'action' : 'indicator_disabled'}, function (value) { indicator_disabled = value; });

// Set links according to the options.
function linkCharacter(element, name, fl, beforeElement)
{
	if(!tmlIcon || !exhIcon)
	{
		console.log("Icons weren't loaded fast enough, forcing load.");
		var send = new Array('tml', 'exh');
		chrome.extension.sendRequest({'icons' : send}, function (value) {
			tmlIcon = value[0];
			exhIcon = value[1];
			if(beforeElement)
				linkCharacter(element, name, fl, beforeElement);
			else
				linkCharacter(element, name, fl);
		});
		return false;
	}
	// Set the float CSS attribute.
	fl = fl || 'none';
	if(fl == 'left')
		var mov = 'right';
	else
		var mov = 'left';
	name = name.replace(/(\xa0|&#160;|&nbsp;| )/g, '+');
	var tml = "";
	var exh = "";
	// Set the icon according to the options.
	if(tmlIcon)
	{
		if(JSON.parse(tmlIcon) !== false)
			tml = '<a href="http://www.tibiaml.com/character/' + name + '"><img alt="Tibia ML" title="Tibia ML" style="height:auto; padding: 0; margin: 0 0 -1px 0; margin-'+mov+': 4px; float: '+fl+'" src="'+chrome.extension.getURL("/images/tibiaml.png") +'"/></a>';
	}	
	else
	{
		tml = '<a href="http://www.tibiaml.com/character/' + name + '"><img alt="Tibia ML" title="Tibia ML" style="height:auto; padding: 0; margin: 0 0 -1px 0; margin-'+mov+': 4px; float: '+fl+'" src="'+chrome.extension.getURL("/images/tibiaml.png") +'"/></a>';
		tmlIcon = true;
	}
	if(exhIcon)
	{
		if(JSON.parse(exhIcon) !== false)
			exh = '<a href="http://www.exhiti.com/experience_history/' + name + '"><img alt="Statistics" title="Exhiti" style="height:auto; padding: 0; margin: 0 0 -1px 0; margin-'+mov+': 4px; float: '+fl+'" src="'+chrome.extension.getURL("/images/exhiti.png") +'"/></a>';
	}	
	else
	{
		exh = '<a href="http://www.exhiti.com/experience_history/' + name + '"><img alt="Statistics" title="Exhiti" style="height:auto; padding: 0; margin: 0 0 -1px 0; margin-'+mov+': 4px; float: '+fl+'" src="'+chrome.extension.getURL("/images/exhiti.png") +'"/></a>';
		exhIcon = true;
	}
	if(beforeElement)	{
		beforeElement.parentNode.insertBefore(element, beforeElement);
	}
	element.innerHTML = element.innerHTML.replace(/^(.+?)\s(<div.+?)$/im,'$1$2');
	element.innerHTML += tml+exh;
	return true;
}

function linkWorlds(element, world, fl)
{
	if(!exhIcon)
	{
		var send = new Array('exh');
		chrome.extension.sendRequest({'icons' : send}, function (value) {
			exhIcon = value[0];
			linkWorlds(element, world, fl);
		});
		return false;
	}
	if(!fl)
		fl = 'none';
	if(fl == 'left')
		var mov = 'right';
	else
		var mov = 'left';
	world = world.replace(/(\xa0|&#160;|&nbsp;| )/g, '+');
	var exh = "";
	if(exhIcon)
	{
		if(JSON.parse(exhIcon) !== false)
			exh = '<a href="http://www.exhiti.com/top_experience/gameworld/' + world + '"><img alt="Statistics" title="Exhiti" style="height:auto; padding: 0; margin: 0 0 -1px 0; margin-'+mov+': 4px; float: '+fl+'" src="'+chrome.extension.getURL("/images/exhiti.png")+'"/></a>';
	}	
	else
	{
		exh = '<a href="http://www.exhiti.com/top_experience/gameworld/' + world + '"><img alt="Statistics" title="Exhiti" style="height:auto; padding: 0; margin: 0 0 -1px 0; margin-'+mov+': 4px; float: '+fl+'" src="'+chrome.extension.getURL("/images/exhiti.png")+'"/></a>';
		exhIcon = true;
	}
	element.innerHTML += exh;
	return true;
}

function reloadTime(mins)
{
	return 1000*60*mins;
}

// Send request to read localStorage from the extension.
function getLocalStorageValue(key)
{
	chrome.extension.sendRequest({'key' : key}, function (value) {
		sessionStorage.setItem(key, value);
	});
}